package com.zygk.qybofficial.view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.flyco.roundview.RoundTextView;
import com.zygk.library.util.StringUtils;
import com.zygk.qybofficial.R;
import com.zygk.qybofficial.model.M_AppInfo;

public final class CommonDialog {

  private CommonDialog() {
  }

  public interface OnYesCallback {
    void onYesClick();
  }

  public interface OnNoCallback {
    void onNoClick();
  }

  public static Dialog showNewAppVersionDialog(Context context, M_AppInfo appInfo,
      final OnYesCallback yesCallback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_new_version_layout, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);

    RoundTextView rtvUpdate = (RoundTextView) layout.findViewById(R.id.rtv_update);
    TextView tvVersion = (TextView) layout.findViewById(R.id.tv_version);
    TextView tvNote = (TextView) layout.findViewById(R.id.tv_note);

    tvVersion.setText("最新版本：" + appInfo.getVersionCode());
    String note = appInfo.getUpdateMessage();
    tvNote.setText(StringUtils.isBlank(note) ? "暂无介绍" : note);
    rtvUpdate.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (yesCallback != null) yesCallback.onYesClick();
      }
    });
    dlg.setCancelable(appInfo.getMustUpdate() == 0 ? true : false);
    dlg.setCanceledOnTouchOutside(appInfo.getMustUpdate() == 0 ? true : false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }
}
